package com.pjb.springbootjjwt.store.storage;

import com.pjb.springbootjjwt.store.Defines;
import com.pjb.springbootjjwt.store.File;
import com.pjb.springbootjjwt.store.entity.Error;
import com.pjb.springbootjjwt.store.entity.Response;
import com.pjb.springbootjjwt.store.utils.FileUtil;
import com.pjb.springbootjjwt.store.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

public class DDL {

    public static void main(String[] args) throws Exception {
//        String name = "mm";
//        new DDL().drop(name);
//        new DDL().create(name, "create table mm(z vchar, primary key(z))");
//        new DDL().desc(name);
        try {
            new DDL().showTables();
        } catch (Exception e) {
            e.printStackTrace();
        }

        new DDL().use("test");
        new DDL().showTables();

    }

//    private void setTables(List<String> list) throws IOException {
//        String c = "";
//        if(!list.isEmpty()) {
//            for(String item: list) {
//                c += item + ",";
//            }
//            c = c.substring(0, c.length() - 1);
//        }
////        FileUtil.write("tablelist.txt", c);
//    }

    private Set<String> getTables() throws Exception {
        return FileUtil.getDir(FileUtil.USE_DB);
    }

    public void showTables() throws Exception {
        System.out.println(getTables());
    }

    public void use(String space) throws Exception {
        FileUtil.use(space);
    }

    /**
     * 创建数据库
     */
    public void createDB(String space) throws Exception {
        FileUtil.makeFolder(space);
    }

    public void dropDB(String space) throws Exception {
        FileUtil.removeFolder(space);
    }

    public void create(String table, String ddl) throws Exception {
        if (ddl.contains("table")) {
            Defines.getHandlerByDDL(ddl).crateTable(table, ddl);
        } else if (ddl.contains("view")) {
            Defines.getHandlerByEngine("view").crateTable(table, ddl);
        } else if (ddl.contains("procedure")) {
            Defines.getHandlerByEngine("procedure").crateTable(table, ddl);
        } else if (ddl.contains("fun")) {
            Defines.getHandlerByEngine("fun").crateTable(table, ddl);
        } else if (ddl.contains("database")) {
            System.out.println("create database ddl:" + ddl);
        } else {
            System.out.println("error:" + ddl);
        }
    }

    public void desc(String table) throws Exception {
        Set<String> list = getTables();
        if (!list.contains(table)) {
            throw new Exception("" + Response.error.set(Error.E90006, table));
        }
        String ddl = File.getInstance().getDDL(table);
        System.out.println(table + ":   " + ddl);
    }

    public void drop(String table) throws Exception {
        drop(table, "table");
    }

    public void drop(String table, String type) throws Exception {
        switch (type) {
            case "table":
                Defines.getHandler(table).dropTable(table);
                break;
            case "view":
                Defines.getHandlerByEngine("view").dropTable(table);
                break;
            case "procedure":
                Defines.getHandlerByEngine("procedure").dropTable(table);
                break;
            case "fun":
                Defines.getHandlerByEngine("fun").dropTable(table);
                break;
        }
    }

    /**
     * mysql-bin.index
     */
    public String showStatus(String command) throws IOException {
        String content = FileUtil.readFile("/", "story-bin.index");
        int num = 0;
        if (!StringUtils.isEmpty(content)) {
            String[] arr = content.split("\r\n");
            String current = arr[arr.length - 1];
            num = Integer.parseInt(current.split("bin.")[1]);
        }
        return "story-bin." + StringUtil.over((byte) '0', 6, num);
    }

    public void flushLogs(String command) throws IOException {
        String content = FileUtil.readFile("/", "story-bin.index");
        List<String> r = new ArrayList<>(Arrays.asList(content.split("\r\n")));
        r.removeAll(Collections.singleton(""));
        int num = 0;
        if (!StringUtils.isEmpty(content)) {
            String[] arr = content.split("\r\n");
            String current = arr[arr.length - 1];
            num = Integer.parseInt(current.split("bin.")[1]);
        }

        String next = "story-bin." + StringUtil.over((byte) '0', 6, ++num);
        r.add("./" + next);
        FileUtil.write("story-bin.index", StringUtils.join(r, "\r\n"));
        FileUtil.write(next, "");
        System.out.println("success.");
    }

    public void purgeBinlog(String command) throws Exception {
        String content = FileUtil.readFile("/", "story-bin.index");
        List<String> r = new ArrayList<>();
        if (!StringUtils.isEmpty(content)) {
            String[] arr = content.split("\r\n");
            for (String k : arr) {
                String name = k.replaceAll("./", "");
                if (StringUtils.isEmpty(name.trim()) || name.equals(command)) {
//                    r += k + "\r\n";
                    r.add(k);
                    continue;
                }
                FileUtil.removeFile("", name);
            }
            FileUtil.write("story-bin.index", StringUtils.join(r, "\r\n"));
        }
    }
}
